﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Unknown.Framework.Database;
using Unknown.Source.Business.Coder;
using Unknown.Source.Business.NormalView;
using Unknown.Source.Entity.Coder;
using Unknown.Source.Entity.NormalView;

namespace Unknown.Source.Application.Builder
{
    /// <summary>
    /// 主窗口
    /// </summary>
    public partial class Major : Form
    {
        private Collection<CascadeTable> _CascadeTables;

        /// <summary>
        /// 构造函数
        /// </summary>
        public Major()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件</param>
        private void Major_Load(object sender, EventArgs e)
        {
            Collection<string> instances = new Collection<string>();
            foreach (KeyValuePair<string, InstanceIdentify> pair in DatabaseHandler.Dictionary)
            {
                instances.Add(pair.Key);
            }
            this.databaseInstanceComboBox.DataSource = instances;

            Collection<string> codeConfigurations = new Collection<string>();
            foreach (KeyValuePair<string, CodeConfiguration> pair in Preference.CodeConfigurations)
            {
                codeConfigurations.Add(pair.Key);
            }
            this.codeConfigurationComboBox.DataSource = codeConfigurations;

            this.databaseLoadButton_Click(this.databaseLoadButton, new EventArgs());
        }

        private void databaseInstanceComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.databaseSourceTextBox.Text = null;

            if (this.databaseInstanceComboBox.SelectedItem != null)
            {
                string key = this.databaseInstanceComboBox.SelectedItem as string;
                bool has = DatabaseHandler.Dictionary.ContainsKey(key);
                if (has)
                {
                    DatabaseHandler.DefaultInstance = key;
                    InstanceIdentify instanceIdentify = DatabaseHandler.Dictionary[key];
                    this.databaseSourceTextBox.Text = instanceIdentify.Source;
                }
            }
        }

        private void databaseLoadButton_Click(object sender, EventArgs e)
        {
            string key = DatabaseHandler.DefaultInstance;
            bool has = DatabaseHandler.Dictionary.ContainsKey(key);
            if (has)
            {
                InstanceIdentify instanceIdentify = DatabaseHandler.Dictionary[key];
                instanceIdentify.Source = this.databaseSourceTextBox.Text;

                CascadeTableBusinessCustom tableBusiness = new CascadeTableBusinessCustom();
                this._CascadeTables = tableBusiness.ExecuteSelect();

                this.filterButton_Click(this.filterButton, new EventArgs());
            }
        }

        private void databaseTableSelectButton_Click(object sender, EventArgs e)
        {
            if (this.databaseTableSelectButton.Tag == null)
            {
                this.databaseTableSelectButton.Tag = false;
            }

            bool select = (bool)this.databaseTableSelectButton.Tag;
            this.databaseTableSelectButton.Tag = !select;
            if (select)
            {
                this.databaseTableSelectButton.Text = "全选";
            }
            else
            {
                this.databaseTableSelectButton.Text = "取消";
            }

            foreach (TreeNode node in this.databaseTableTreeView.Nodes)
            {
                node.Checked = !select;
            }
        }

        private void buildButton_Click(object sender, EventArgs e)
        {
            this.buildButton.Enabled = false;

            if (this.codeConfigurationComboBox.SelectedItem != null)
            {
                string key = this.codeConfigurationComboBox.SelectedItem as string;
                
                foreach (TreeNode node in this.databaseTableTreeView.Nodes)
                {
                    if (node.Checked)
                    {
                        CascadeTable cascadeTable = node.Tag as CascadeTable;
                        if (cascadeTable != null)
                        {
                            CodeHandler.Build(cascadeTable, key, Entrance.ConfigurationDirectory);
                        }
                    }
                }
            }

            this.buildButton.Enabled = true;
        }

        private void filterButton_Click(object sender, EventArgs e)
        {
            this.databaseTableTreeView.Nodes.Clear();

            if (this._CascadeTables != null)
            {
                foreach (CascadeTable cascadeTable in this._CascadeTables)
                {
                    if (cascadeTable.Table.Name.ToLower().IndexOf(this.filterTextBox.Text.ToLower()) >= 0)
                    {
                        TreeNode node = new TreeNode();
                        node.Name = cascadeTable.Table.Name;
                        node.Text = string.Format("{0} - {1}", cascadeTable.Table.Name, cascadeTable.Table.Remark);
                        node.Tag = cascadeTable;

                        this.databaseTableTreeView.Nodes.Add(node);
                    }
                }
            }
        }
    }
}
